package com.example.rabbitmqspringboot.controller;


import com.example.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/work")
    public String work(){
        // 内置交换机,RoutingKey和队列名称一致
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"hello,这是一条消息!这是第"+i+"条!");
        }
        return "发送成功!";
    }

    @RequestMapping("/fanout")
    public String fanout(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "","hello,这是一条消息!这是第"+i+"条!");
        }
        return "发送成功!";
    }

    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey,"hello,这是一条消息!发送到"+routingKey+"队列");
        return "发送成功!";
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey,"hello,这是一条消息!发送到"+routingKey+"队列");
        return "发送成功!";
    }
}
